/**
 *  文件名:HomeActivity.java
 *  修改人:lihangjie
 *  创建时间:2015-9-3 下午4:18:17
 */
package com.sharemob.tinchat.modules.account;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Window;

import com.sharemob.tinchat.R;
import com.sharemob.tinchat.base.BaseActivity;
import com.sharemob.tinchat.base.Callback;
import com.sharemob.tinchat.modules.dao.cache.CacheManager;
import com.sharemob.tinchat.modules.launch.MainActivity;
import com.sharemob.tinchat.thirdparty.weixin.WXConstant;
import com.sharemob.tinchat.thirdparty.weixin.WXUserInfo;

/**
 * 
 * <一句话功能简述>
 *
 * @author lihangjie
 * version [版本号,2015-9-3 下午4:18:17]
 * @see    [相关类/方法]
 * @since  [产品/模块版本]
 *
 */
public class HomeActivity extends BaseActivity {

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_home);
        
        //添加微信授权
		WXConstant.api.registerApp(WXConstant.AppID);
        registerWinXinAuth();
        BtnEvent(R.id.btn_login_login,new Callback() {
			public void doDown() {
				gotoViewByAnim(HomeActivity.this,LoginActivity.class,R.anim.activity_open,true);
			}
		});
        
        BtnEvent(R.id.btn_login_register,new Callback() {
			public void doDown() {
				gotoViewByAnim(HomeActivity.this,RegisterActivity.class,R.anim.activity_open,false);
			}
		});
        
        BtnEvent(R.id.iBtn_login_wechat,new Callback() {
   			public void doDown() {
   				WXConstant.WXSendAuthLogin();
				
//				WXConstant.SharedTextWXFriend("灵刃传说","helloworld");
				
//				WXConstant.SharedWebviewToWX("http://www.baidu.com", "灵刃传说", "asdads灵刃传说", R.drawable.icon108, WXConstant.WXFriend);
			
   			}
   		});
        
        BtnEvent(R.id.iBtn_login_qq,new Callback() {
     			public void doDown() {
     				
     			}
     		});
        
    	CacheManager.getInstance().PhoneLogin();
    	gotoViewByAnim(this, MainActivity.class, R.anim.in_from_zoom,true);
    }

	protected void keyEventOfListView(int position, int id) {
		
	}
	
	/** 
     * 关闭Activity的广播，放在自定义的基类中，让其他的Activity继承这个Activity就行 
     */  
    protected BroadcastReceiver wxauthreceiver = new BroadcastReceiver() {  
        	public void onReceive(Context context, Intent intent) {
        		String action=intent.getAction();
        		System.out.println(action);
        		  if(action.equals(WXConstant.ACTION_WXAUTH_LOGIN)){
        			  System.out.println("---action:"+action);
        			  Bundle bundle=intent.getExtras();
        			  
        			  WXUserInfo userInfo=(WXUserInfo) bundle.getSerializable("WXUser");
//        			  System.out.println("---user:"+userInfo.getNickname());
        			  CacheManager.getInstance().WeiXinLogin(userInfo);
        			  
        			  Intent sintent = new Intent(HomeActivity.this, MainActivity.class);
        			  sintent.putExtras(bundle);
        		      context.startActivity(sintent);
        		      overridePendingTransition( R.anim.in_from_zoom,0);
        		  }
        		   finish();  
        	}
    }; 

	// 在当前的activity中注册广播  
	protected void registerWinXinAuth(){
        IntentFilter filter = new IntentFilter();  
        filter.addAction(WXConstant.ACTION_WXAUTH_LOGIN);  
        registerReceiver(wxauthreceiver, filter);
	}
	
    protected void onDestroy() {  
        super.onDestroy();  
        this.unregisterReceiver(this.wxauthreceiver);  
    }  
}
